package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class PostPeer extends BasePeer{
	public static final String tableName = "Post";
	public static final String POSTID = "postId";
	public static final String TITLE = "title";
	public static final String USERID = "userId";
	public static final String REPLIEDID = "repliedId";
	public static final String USERNAME = "username";
	public static final String CONTENT_S = "content_s";
	public static final String CONTENT_R = "content_r";
	public static final String CONTENT_P = "content_p";
	public static final String CONTENT_A = "content_a";
	public static final String POSTTIME = "postTime";
	public static final String ORDERWEIGHT = "orderWeight";
	public static final String REPLYPOLICY = "replyPolicy";
	public static final String STATE = "state";
	public static final String SCORENEGATIVE = "scoreNegative";
	public static final String SCOREPOSITIVE = "scorePositive";
	public static final String LASTMODIFYTIME = "lastModifyTime";
	public static final String LASTREPLYTIME = "lastReplyTime";
	public static final String CATEGORIES = "categories";
	public static final String REPLIES = "replies";
	public static final String READS = "reads";
	public static final String WORDCOUNT = "wordCount";
	public static final String[] columnNames=new String[]{"postId","title","userId","repliedId","username","content_s","content_r","content_p","content_a","postTime","orderWeight","replyPolicy","state","scoreNegative","scorePositive","lastModifyTime","lastReplyTime","categories","replies","reads","wordCount"};
	public static Post getPostByPKs(Object postId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(PostPeer.tableName,PostPeer.columnNames," TRUE  AND `postId`=? ",new Object[]{postId,},0, 1,conn);
			Post post = new Post();
		fill(rs,post);
		return post;
	}
	public static Post getPostReferenceColumnsOnlyByPKs(Object postId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(PostPeer.tableName,new String[]{"postId",}," TRUE AND `postId`=?",new Object[]{postId,}, 0,  1,conn);
			Post post = new Post();
		fill(rs,post);
		return post;
	}
	public static Post getPostByPostId(long postId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(PostPeer.tableName,PostPeer.columnNames,"`postId`=?",new Object[]{postId},0, 1,conn);
			Post post = new Post();
		fill(rs,post);
		return post;
	}
	public static void fill(ResultSet rs, Post post) throws SQLException {
		if(rs.next()){
			try{post.setPostId(rs.getLong("postId"));}catch(Exception ex){}
			try{post.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{post.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{post.setRepliedId(rs.getLong("repliedId"));}catch(Exception ex){}
			try{post.setUsername(rs.getString("username"));}catch(Exception ex){}
			try{post.setContentS(rs.getString("content_s"));}catch(Exception ex){}
			try{post.setContentR(rs.getString("content_r"));}catch(Exception ex){}
			try{post.setContentP(rs.getString("content_p"));}catch(Exception ex){}
			try{post.setContentA(rs.getString("content_a"));}catch(Exception ex){}
			try{post.setPostTime(rs.getTimestamp("postTime"));}catch(Exception ex){}
			try{post.setOrderWeight(rs.getInt("orderWeight"));}catch(Exception ex){}
			try{post.setReplyPolicy(rs.getInt("replyPolicy"));}catch(Exception ex){}
			try{post.setState(rs.getInt("state"));}catch(Exception ex){}
			try{post.setScoreNegative(rs.getLong("scoreNegative"));}catch(Exception ex){}
			try{post.setScorePositive(rs.getLong("scorePositive"));}catch(Exception ex){}
			try{post.setLastModifyTime(rs.getTimestamp("lastModifyTime"));}catch(Exception ex){}
			try{post.setLastReplyTime(rs.getTimestamp("lastReplyTime"));}catch(Exception ex){}
			try{post.setCategories(rs.getString("categories"));}catch(Exception ex){}
			try{post.setReplies(rs.getLong("replies"));}catch(Exception ex){}
			try{post.setReads(rs.getLong("reads"));}catch(Exception ex){}
			try{post.setWordCount(rs.getLong("wordCount"));}catch(Exception ex){}
			post.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<Post> postCollection) throws SQLException {
		while(rs.next()){
			Post post=new Post();
			try{post.setPostId(rs.getLong("postId"));}catch(Exception ex){}
			try{post.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{post.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{post.setRepliedId(rs.getLong("repliedId"));}catch(Exception ex){}
			try{post.setUsername(rs.getString("username"));}catch(Exception ex){}
			try{post.setContentS(rs.getString("content_s"));}catch(Exception ex){}
			try{post.setContentR(rs.getString("content_r"));}catch(Exception ex){}
			try{post.setContentP(rs.getString("content_p"));}catch(Exception ex){}
			try{post.setContentA(rs.getString("content_a"));}catch(Exception ex){}
			try{post.setPostTime(rs.getTimestamp("postTime"));}catch(Exception ex){}
			try{post.setOrderWeight(rs.getInt("orderWeight"));}catch(Exception ex){}
			try{post.setReplyPolicy(rs.getInt("replyPolicy"));}catch(Exception ex){}
			try{post.setState(rs.getInt("state"));}catch(Exception ex){}
			try{post.setScoreNegative(rs.getLong("scoreNegative"));}catch(Exception ex){}
			try{post.setScorePositive(rs.getLong("scorePositive"));}catch(Exception ex){}
			try{post.setLastModifyTime(rs.getTimestamp("lastModifyTime"));}catch(Exception ex){}
			try{post.setLastReplyTime(rs.getTimestamp("lastReplyTime"));}catch(Exception ex){}
			try{post.setCategories(rs.getString("categories"));}catch(Exception ex){}
			try{post.setReplies(rs.getLong("replies"));}catch(Exception ex){}
			try{post.setReads(rs.getLong("reads"));}catch(Exception ex){}
			try{post.setWordCount(rs.getLong("wordCount"));}catch(Exception ex){}
			post.resetModified();
			postCollection.add(post);
		}
	}
	public static boolean update(Post post, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < PostPeer.columnNames.length; i++) {
			if (((PostBase)post).modified[i]) {
				columns.add(PostPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `Post` SET ");
		sql.append("WHERE TRUE AND `postId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,post.get(column));
		}
		pstmt.setObject(columns.size()+1+0,post.get("postId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(Post post, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (post.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `Post` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (post.get(column) != null) {
				pstmt.setObject(i + 1 + j, post.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<Post> getPostCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<Post> collection=new Vector<Post>();
		fill(rs,collection);
		return collection;
	}
}
